package com.lanou3g.demo.service.roleInfo;

import com.lanou3g.demo.entity.ModuleInfo;
import com.lanou3g.demo.entity.RoleInfo;
import com.lanou3g.demo.entity.RoleModule;
import com.lanou3g.demo.entity.ex.RoleWithModule;
import com.lanou3g.demo.mapper.RoleInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleInfoService implements IRoleInfoService {


    private static RoleInfoMapper roleInfoMapper;

    @Autowired
    public RoleInfoService(RoleInfoMapper roleInfoMapper){
        RoleInfoService.roleInfoMapper = roleInfoMapper;
    }

    /**
     * 查询出角色的数量
     * @return
     */
    @Override
    public int roleQuantity() {
        return roleInfoMapper.roleQuantity();
    }

    /**
     * 查询出角色和对应能操作的模块的信息
     * @return
     */
    @Override
    public List<RoleWithModule> findRoleWithModule(int page,int count) {
        Map<String, Integer> map = new HashMap<>();

        try {
            page = (page -1) * count;
        } catch (Exception e) {
            page = 1;
            count = 3;
        }finally {

            map.put("page",page);
            map.put("count",count);
        return  roleInfoMapper.findRoleWithModule(map);
        }
    }


    /**
     * 查询所有的角色信息
     * @return
     */
    @Override
    public List<RoleInfo> findAllRole() {
        return roleInfoMapper.findAllRole();
    }

    /**
     * 根据Id查询角色信息
     * @param roleId
     * @return
     */
    @Override
    public RoleInfo findRoleById(Integer roleId) {
        return roleInfoMapper.findRoleById(roleId);
    }

    /**
     * 增加一条角色的信息
     * @param roleName
     * @param allModule
     * @return
     */
    @Override
    // 事务的添加:delete,update,add
     @Transactional
    public int addOneRole(String roleName, String allModule) {
        RoleInfo role = new RoleInfo();
        role.setRoleName(roleName);
        int i = roleInfoMapper.addOneRole(role);
        Integer roleId = role.getRoleId();

        if (allModule != null){
            addModule(roleId,allModule);
        }
        return i;
    }


    /**
     * 删除一个角色对应的模块(role_module表)
     * @param roleId
     * @return
     */
    @Override
    @Transactional
    public int delRoleWithModule(Integer roleId) {
        int i = roleInfoMapper.delRole(roleId);
        System.out.println("Service层删除id:" + roleId);
        roleInfoMapper.delRoleModule(roleId);

        return i;
    }



    /**
     * 更新一条角色信息
     * @param roleWithModule
     * @return
     */
    @Override
    @Transactional
    public int udpRoleWithModule(RoleWithModule roleWithModule) {
        // 按照id的映射器
        Integer roleId = roleWithModule.getRoleId();
        RoleInfo infoMapperRoleById = roleInfoMapper.findRoleById(roleId);

        String roleName = roleWithModule.getRoleName();
        String allModule = roleWithModule.getAllModule();
        roleInfoMapper.delRoleModule(roleId);

        // 将修改的Id与Name添加到Map中
        if (!infoMapperRoleById.getRoleName().equals(roleName)){
            Map<String,Object> map = new HashMap<>();
            map.put("roleId",roleId);
            map.put("roleName",roleName);
            roleInfoMapper.updRole(map);
        }
        // 判断:如果所有模块在非空的情况下,添加角色模块
        if(allModule != null){
            addModule(roleId,allModule);
        }
        return 0;
    }

    /**
     * 添加角色模块
     * @param roleId
     * @param allModule
     */
    public static void addModule(Integer roleId,String allModule){
        // 将模块信息按照,分开,进行遍历,获取到角色的Id与与赋予的模块权限,
        // 增加更新分别可以使用这一角色模块
        String[] modules= allModule.split(",");
        for (String module : modules) {
            RoleModule roleModule = new RoleModule();
            roleModule.setRoleId(roleId);
            roleModule.setModuleId(Integer.parseInt(module));
            roleInfoMapper.addRoleOneModule(roleModule);
        }
    }
}
